package guiTest;

import java.util.Random;

public class MP3_Test_Status 
{
	// Play Modes
	public static final int MODE_LOOP = 1;
	public static final int MODE_REPEAT = 2;
	public static final int MODE_RANDOM = 3;
	public static final int MODE_REVERSE_LOOP = 4;
	
	boolean paused;
	boolean newSong;
	boolean isAfterDrag;
	boolean isDuringDrag;
	
	double songPercent;
	int playMode;
	Song currentSong;
	Random r;
	SongData songData;
	GUI_Creator parent;
	long startTime;
	long labelSeconds;
	long offset;
			
	public MP3_Test_Status(SongData sd, GUI_Creator gc)
	{
		songData = sd;
		r = new Random();
		paused = false;
		newSong = true;
		isAfterDrag = false;
		isDuringDrag = false;
		playMode = MODE_RANDOM;
		currentSong = null;
		parent = gc;
		songPercent = 0.0;
		requestRandomSong();
	}
	
	public synchronized void setMode(int newMode)
	{
		playMode = newMode;
	}
	
	public synchronized int getMode()
	{
		return playMode;
	}
	
	public synchronized void pause()
	{
		paused = true;
		offset += (System.currentTimeMillis() - startTime);
	}
	
	public synchronized void resume()
	{
		paused = false;
		startTime = System.currentTimeMillis();
		notifyAll();
	}
	
	public synchronized boolean isPaused()
	{
		return paused;
	}
	
	public synchronized void setNewSong()
	{
		newSong = true;		
	}
	
	public synchronized void setNotNewSong()
	{
		newSong = false;	
		notifyAll();
	}
	
	public synchronized boolean isNewSong()
	{
		return newSong;		
	}	
	
	public synchronized String getCurrentSongPath()
	{
		String path = SongData.MAIN_DIR + "\\" + currentSong.getBandFolderName() + 
			"\\" + currentSong.getFilename();
		
		return path;
	}
	
	public synchronized long getCurrentSongDuration()
	{
		return (long)(currentSong.getDuration().getDurationVal());
	}
	
	public synchronized void requestRandomSong()
	{
		currentSong = songData.getRandomSong();
		newSong = true;
				
		parent.updateLabels(currentSong);
		
		startTime = System.currentTimeMillis();
		labelSeconds = 0;
		offset = 0;
		parent.updateTimeLabel((int)labelSeconds, 
			currentSong.getDuration().getDurationInSeconds());
		parent.updateSlider(0);
	}
	
	public synchronized void requestNextSong()
	{
		int currentSongNumber = currentSong.getSongNumber();
		int nextSongNumber = currentSongNumber + 1;
		
		if(nextSongNumber + 1 >= songData.getNumberOfSongs())
		{
			nextSongNumber = 0;			
		}
		
		currentSong = songData.getSong(nextSongNumber);
		newSong = true;
		parent.updateLabels(currentSong);
		
		startTime = System.currentTimeMillis();
		labelSeconds = 0;
		offset = 0;
		parent.updateTimeLabel((int)labelSeconds,
			currentSong.getDuration().getDurationInSeconds());
		parent.updateSlider(0);
	}
	
	public synchronized void requestSong(int songNum)
	{
		currentSong = songData.getSong(songNum);
		newSong = true;
		parent.updateLabels(currentSong);
		
		startTime = System.currentTimeMillis();
		labelSeconds = 0;
		offset = 0;
		parent.updateTimeLabel((int)labelSeconds,
			currentSong.getDuration().getDurationInSeconds());
		parent.updateSlider(0);
	}
	
	public synchronized void requestPreviousSong()
	{
		int currentSongNumber = currentSong.getSongNumber();
		int nextSongNumber = currentSongNumber - 1;
		
		if(nextSongNumber < 0)
		{
			nextSongNumber = songData.getNumberOfSongs() - 1;			
		}
		
		currentSong = songData.getSong(nextSongNumber);
		newSong = true;
		parent.updateLabels(currentSong);
		
		startTime = System.currentTimeMillis();
		labelSeconds = 0;
		offset = 0;
		parent.updateTimeLabel((int)labelSeconds,
			currentSong.getDuration().getDurationInSeconds());
		parent.updateSlider(0);
	}

	public synchronized void updateTime(double percentage)
	{
		int totalMillis = currentSong.getDuration().getDurationVal();
		int currentStateMillis = (int)(totalMillis * percentage);
		
		startTime = System.currentTimeMillis();
		labelSeconds = (long) (currentStateMillis / 1000);
		offset = (long)currentStateMillis;
		
		parent.updateTimeLabel((int)labelSeconds, 
			currentSong.getDuration().getDurationInSeconds());
		//parent.updateSlider(0);		
	}
	
	public synchronized void addTime() 
	{
		if(!isPaused())
		{
			long songTimeMilliseconds = 
				System.currentTimeMillis() - startTime + offset;
			
		//	System.out.println("here: " + songTimeMilliseconds);
			
			long x = songTimeMilliseconds / 1000;
			
			if(x != labelSeconds)
			{
				labelSeconds = x;
				parent.updateTimeLabel((int)labelSeconds,
					currentSong.getDuration().getDurationInSeconds());
			}
			
			parent.updateSlider(songTimeMilliseconds);
		}
	}

	public synchronized int getCurrentSongNumber()
	{
		return currentSong.getSongNumber();
	}

	public synchronized boolean isAfterDrag() 
	{
		return isAfterDrag;
	}
	
	public synchronized boolean isDuringDrag() 
	{
		return isDuringDrag;
	}

	public synchronized void setIsAfterDrag(boolean b) 
	{
		isAfterDrag = b;		
	}

	public synchronized void setIsDuringDrag(boolean b) 
	{
		isDuringDrag = b;
	}

	public synchronized void setPercent(double percent)
	{
		songPercent = percent;	
	}
	
	public synchronized double getPercent()
	{
		return songPercent;
	}
	
	public String getStatusStr()
	{
		String state = /*"paused: " + paused + ", newSong: " + newSong + ", isAfterDrag: " + 
		    isAfterDrag + ", isDuringDrag: " + isDuringDrag +*/ "songPercent: " +
		    songPercent + ", startTime: " + startTime + ", offset: " + offset;
		
		return state;
	}
}
